//
//  AddNewFriendVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/2.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "AddNewFriendVC.h"
#import "YBApiManager+Friend.h"
#import "FriendInfoListVC.h"
#import "DIYScanVC.h"
#import "UIViewController+ScanQR_Logic.h"

@interface AddNewFriendVC ()
@property (weak, nonatomic) IBOutlet UITextField *searchTF;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;

@property (weak, nonatomic) IBOutlet UIButton *qrBtn;

@end

@implementation AddNewFriendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)setupUI {
    UIImageView *searcImgView = [[UIImageView alloc] initWithImage:UIImage.k_imgName(@"new_search")];
    searcImgView.frame = CGRectMake(0, 0, 25, 25);
    self.searchTF.leftView = searcImgView;
    self.searchTF.leftViewMode = UITextFieldViewModeAlways;
    
    self.searchBtn.k_cornerRadius(5.0f);
}

- (IBAction)searchBtnAction:(id)sender {
    if (self.searchTF.text.isHaveStr) {
        @weakify(self);
        [MBProgressHUD showActivityMessageInView:@""];
        [[YBApiManager.shareManager searchUserWithWord:self.searchTF.text] subscribeNext:^(id  _Nullable x) {
            [MBProgressHUD hideHUD];
            NSDictionary *dataDic = [x objectForKey:@"data"];
            
            FriendItem *u = [FriendItem mj_objectWithKeyValues:dataDic];
            @strongify(self);
            [self showUserDetailWithUser:u];
            
        } error:^(NSError * _Nullable error) {
            [MBProgressHUD hideHUD];
            if ([error isServerError]) {
                [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
            } else {
                [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
            }
        }];
    }
}

- (IBAction)qrBtnAction:(id)sender {
    
    [self qr_showScanQRVC];
//    DIYScanVC *scanVC = [DIYScanVC new];
//    scanVC.scanResult = ^(NSString *result) {
//        [self showFriendInfoVCWithType:2 friendItem:[FriendItem mj_objectWithKeyValues:result] from:@"1"];
//    };
//    [self.navigationController pushViewController:scanVC animated:YES];
}

- (void)showUserDetailWithUser:(FriendItem *)user {
    FriendInfoListVC *infoVc = [FriendInfoListVC new];
    infoVc.user = user;
    infoVc.from = @"0";
    [self.navigationController pushViewController:infoVc animated:YES];
}
- (void)showFriendInfoVCWithType:(NSInteger)type friendItem:(FriendItem *)item from:(NSString *)from {
    FriendInfoListVC *VC = [FriendInfoListVC new];
    VC.user = item;
    VC.from = from;
    VC.listType = type;
    
    [self.navigationController pushViewController:VC animated:YES];
}
@end
